﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Linq;


namespace QuanLyThuVien.DAO
{
    class TheLoaiDAO
    {
        public static IEnumerable<TheLoai> GetAllTL()
        {
            NorthwindDataContext context = new NorthwindDataContext();
            IEnumerable<TheLoai> sql = from TL in context.TheLoais select TL;
            return sql;
        }

        public static void Them(TheLoai TL)
        {
            NorthwindDataContext context = new NorthwindDataContext();
            TheLoai a = new TheLoai();
            a.TenTheLoai = TL.TenTheLoai;
            context.TheLoais.InsertOnSubmit(a);
            context.SubmitChanges();
        }

        public static void Sua(TheLoai TL)
        {

            NorthwindDataContext context = new NorthwindDataContext();
            TheLoai a = context.TheLoais.Single(stloai => stloai.MaTheLoai == TL.MaTheLoai);
            a.TenTheLoai = TL.TenTheLoai;
            context.SubmitChanges();
        }

        public static DataGridView GetAllTheLoai()
        {
            NorthwindDataContext db = new NorthwindDataContext();
            DataGridView dt = new DataGridView();
            var sql = from TG in db.TheLoais select TG;
            dt.DataSource = sql;
            return dt;
        }

        // kiem tra the loai
        public static int KT_TheLoai(string ten)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            var ds = from tl in db.TheLoais
                     where tl.TenTheLoai == ten
                     select tl;
            if (ds.Count() > 0)
            {
                return 1;
            }
            return 0;
        }
    }
}
